package com.wyl.libbase.base

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.view.menu.MenuBuilder
import com.google.gson.JsonSyntaxException
import com.lzy.okgo.exception.HttpException
import com.lzy.okgo.exception.StorageException
import com.wyl.easydialog.EasyDialog
import com.wyl.easydialog.EasyDialogUtil
import com.wyl.easydialog.R
import com.wyl.libbase.utils.ActivityStack
import com.wyl.libbase.utils.KLog
import io.reactivex.disposables.CompositeDisposable
import org.json.JSONException
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.net.UnknownHostException


abstract class BaseActivity : AppCompatActivity(), Presenter {
    override fun onCreate(savedInstanceState: Bundle?) {
        ActivityStack.screenManager.pushActivity(this)
        KLog.d("before")
        super.onCreate(savedInstanceState)
        KLog.d("after")
        setContentView()
        initView()
        loadData()
    }

    override fun onContentChanged() {
        KLog.d("before")
        super.onContentChanged()
        KLog.d("after")

    }

    abstract fun setContentView()

    abstract fun getLayoutId(): Int

    abstract fun initView()

    abstract fun loadData()

    override fun onClick(v: View) {}

    private val loadingDialog by lazy(EasyDialogUtil::showLoadingDialog)

    protected fun showLoadingDialog() = loadingDialog.show(supportFragmentManager)

    protected fun dismissLoadingDialog() = loadingDialog.dismiss()

    /** 让menu条目的icon可见 */
    @SuppressLint("RestrictedApi")
    override fun onPrepareOptionsMenu(menu: Menu): Boolean {
        val builder = menu as MenuBuilder
        builder.setOptionalIconsVisible(true)
        return true
    }

    protected val disposables by lazy(::CompositeDisposable)

    override fun onDestroy() {
        disposables.clear()
        super.onDestroy()
    }

    // Toolbar左侧返回键处理
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return if (item.itemId == android.R.id.home) {
            onBackPressed()
            true
        } else {
            super.onOptionsItemSelected(item)
        }
    }

}